﻿//#define TEST
#define DEBUG
#define RELEASE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Threading;
using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    public partial class Form1 : Form
    {
        private Utility utl = null;             // 作業中の関する補助
        private IWorkspace wkspObject = null;   // 作業オブジェクト
        private Thread trdExecute = null;       // マルチストレート

        /// <summary>
        /// マルチストレートを基づき、実行中随時に元のプロシージャへ戻れる
        /// </summary>
        private delegate void ReturnToInitialHandler();

        /// <summary>
        /// フォールの表示と伴って、いろいろな初期化を行う
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "SMJファイル(*.smw)|*.smw|すべてのアイル(*.*)|*.*";
            button4.Enabled = false;
        }

        /// <summary>
        /// 処理元を選択
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                textBox1.Text = filePath;
                textBox2.Text = filePath.Substring(0, filePath.LastIndexOf('\\')) + Utility.WORKING_DIR_DEFAULT;
            }
        }

        /// <summary>
        /// 保存先を指定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = folderBrowserDialog1.SelectedPath + Utility.WORKING_DIR_DEFAULT;
            }
        }

        /// <summary>
        /// 変換開始次第、マルチストレートも実行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                ctl.Enabled = false;
            }
            button4.Enabled = true;

            trdExecute = new Thread(new ThreadStart(Execute));
            trdExecute.Start();
        }

        /// <summary>
        /// マルチストレートで、いつでも実行の取消することできる
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            button4.Enabled = false;

            trdExecute.Abort();
            trdExecute.Join();

            ReturnToInitial();
        }

        /// <summary>
        /// 線路とラベルを分ける機能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            button5.Enabled = false;

            try
            {
                Initialize();

                RouteDivision division = new RouteDivision(axSuperWorkspace1);
                division.MakeDivision();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            button5.Enabled = true;
        }

        /// <summary>
        /// ワークスペースを初期化
        /// </summary>
        private void Initialize()
        {
            utl = new Utility(axSuperWorkspace1);
            utl.InitializeWksp(textBox1.Text, textBox2.Text);
        }

        /// <summary>
        /// 正式な実行開始
        /// </summary>
        private void Execute()
        {
            /*初期化*/
            try
            {
                Initialize();
            }
            catch (ThreadAbortException ae)
            {
                MessageBox.Show(Utility.MSG_MANUALLY_STOP, ae.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                Invoke(new ReturnToInitialHandler(ReturnToInitial));

                return;
            }

            /*バッファー用データセットを作成*/
#if RELEASE
            try
            {
#endif
                wkspObject = new RouteDataset(axSuperWorkspace1);
                wkspObject.Initialize();
                wkspObject.SaveToWksp();
#if RELEASE
            }
            catch (ThreadAbortException ae)
            {
                MessageBox.Show(Utility.MSG_MANUALLY_STOP, ae.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                Invoke(new ReturnToInitialHandler(ReturnToInitial));

                return;
            }
#endif

            /*バッファーデータセットを無事で終了次第、マップ(レイヤーも)とレイアウトを作成できる*/
#if RELEASE
            if (!axSuperWorkspace1.Datasources[Utility.s_DatasourceAlias].IsAvailableDatasetName(Utility.s_NewDatasetName))
            {
#endif
                /*マップ(レイヤーも)を作成*/
                try
                {
                    wkspObject = new BufferLayer(axSuperWorkspace1, axSuperMap1);
                    wkspObject.Initialize();
                    wkspObject.SaveToWksp();
                    wkspObject.ReleaseMemory();
                }
                catch (ThreadAbortException ae)
                {
                    MessageBox.Show(Utility.MSG_MANUALLY_STOP, ae.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                    Invoke(new ReturnToInitialHandler(ReturnToInitial));

                    return;
                }

                /*レイアウトを作成*/
                try
                {
                    wkspObject = new ZoneLayout(axSuperWorkspace1, axSuperLayout1);
                    wkspObject.Initialize();
                    wkspObject.SaveToWksp();
                    //wkspObject.OutputAsFile();
                    wkspObject.ReleaseMemory();
                }
                catch (ThreadAbortException ae)
                {
                    MessageBox.Show(Utility.MSG_MANUALLY_STOP, ae.Message);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                    Invoke(new ReturnToInitialHandler(ReturnToInitial));

                    return;
                }
            }

            /*無事で終了すれば、元のストレートを呼び出し*/
            if (InvokeRequired)
            {
                Invoke(new ReturnToInitialHandler(ReturnToInitial));
            }
        }

        /// <summary>
        /// 元のストレートへ戻り、リソースも解放
        /// </summary>
        private void ReturnToInitial()
        {
            utl.Dispose();

            foreach (Control ctl in this.Controls)
            {
                ctl.Enabled = true;
            }
            button4.Enabled = false;
        }
    }
}